package com.springl.el;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

/**
 *
* @ClassName: ELConfig
* @Description: TODO(演示配置类)
* @author antch,861710865@qq.com
* @date 2017年7月13日 下午3:02:53
*
 */
@Configuration
@ComponentScan("com.springl.el")
@PropertySource("classpath:com/springl/el/text.properties")
public class ELConfig {

	//注入普通字符串
	@Value("I love you")
	private String normal;

	//注入操作系统属性
	@Value("#{systemProperties['os.name']}")
	private String osName;

	//注入表达式结果
	@Value("#{T(java.lang.Math).random()*100.0}")
	private double randomNum;

	//注入其他bean的属性
	@Value("#{demoService.another}")
	private String fromAnother;

	//注入文件资源
	@Value("classpath:com/springl/el/text.txt")
	//使用此注入文件资源，需要配置一个bean：：：PropertySourcesPlaceholderConfigurer
	private Resource textFile;

	//注入网址资源
	@Value("http://www.baidu.com")
	private Resource testUrl;

	//注入配置文件
	@Value("${book.name}")
	private String bookName;

	@Autowired
	private Environment environment;

	@Bean
	public static PropertySourcesPlaceholderConfigurer propertyConfigur() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	@SuppressWarnings("deprecation")
	public void outputResource() throws IOException {
		System.out.println("normal:" + normal);
		System.out.println("osName:" + osName);

		System.out.println("randomNum:" + randomNum);
		System.out.println("fromAnother:" + fromAnother);
		System.out.println("textFile:" + IOUtils.toString(textFile.getInputStream()) );
		System.out.println("testUrl:" + IOUtils.toString(testUrl.getInputStream()));
		System.out.println("bookName:" + bookName);

		System.out.println("environment:" + environment.getProperty("book.author"));

	}
}
